#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import os
import time
from concurrent.futures import ProcessPoolExecutor


def tesk(res):
    time.sleep(5)
    print("进程: {} 结果: {}".format(os.getpid(), res))
    # return res ** 2
    return parse(res)

def parse(res):
    return res ** 2


if __name__ == '__main__':
    pool = ProcessPoolExecutor(4)
    l = []
    start = time.time()
    for i in range(1, 5):
        func = pool.submit(tesk, i)
        # print(func.result())  # 直接取到结果,会变成同步, 每次都行返回结果, 如同 串行
        l.append(func)

    for fu in l:
        print(fu.result())
    pool.shutdown(wait=True)
    stop = time.time()

    print("master: {}, 时间: {}".format(os.getpid(), stop - start))
    # master: 24272, 时间: 5.320957899093628
